Date and time routines
Date and time are very important in most programs, so that we will explain the most
important functions about date and time.
TDateTime
TDateTime is a type used to declare date, time, or date-time variable. TDateTime is
a Double precision-floating point number, so that you can assign TDateTime variable
to a Double variable or vice versa such as:
Var
MyDate: TDateTime;
SecondDate: Double;
begin
MyDate:= Now;
SecondDate:= MyDate
TDateTime is splitted into two part: integral part which represents date; and fraction
part which represents time. TDateTime value represented by days, for example:
CDate == Today
CDate + 1 == Tomorrow
CDate - 1 == Yesterday
CDate + 7 == Next week
CDate - 7 == Last week
Note:
Suppose that CDate is a TDateTime variable and it represents current date.
As I've said, time represented by fraction portion of TDateTime type, so that if
we want to add or substract hours we must divide it by 24, minutes by 24*60, and
seconds by 24*60*40, for example:
CTime == Current time
CTime + 1/24 == Next hour
CTime + 1/(24*60) == Next minute
CTime - 1/(24*60*60) == Last second
CTime + 2/24 == Next two hours
CTime + 0.5 == Add half a day (12 hours: 12/24 = 0.5)
CTime + 24/24 == Add a day (24 hour) or can be CTime + 1
CTime - 1.5 == Substract day and 12 hours
Note:
Suppose that CTime is a TDateTime variable and it represents current time.
Now
Now is a function that returns current date and time, for example:
var
CurrentDateTime: TDateTime;
begin
CurrentDateTime:= Now;
Date
Date is a function that returns current date, for example:
var
CDate: TDateTime;
begin
CDate:= Date;
Time
Time is a function that returns current time, for example:
var
CTime: TDateTime;
begin
CTime:= Time;
DateTimeToStr
Converts TDateTime to string, for example:
ShowMessage(DateTimeToStr(Now));
The output can be:
08/10/99 07:51:28
DateToStr
Converts date portion of TDateTime to string, for example:
ShowMessage(DateToStr(Date));
The output can be:
08/10/99
TimeToStr
Converts Time portion of TDateTime to string, for example:
ShowMessage(TimeToStr(Time));
The output can be:
07:54:24
DecodeDate
Extracts Year, Month, and Day values from TDateTime variable, for example:
var
Year, Month, Day: word;
DateStr: string;
begin
DecodeDate(Date, Year, Month, Day);
DateStr:= IntToStr(Day)+'/'+IntToStr(Month)+'/'+IntToStr(Year);
ShowMessage(DateStr);
end;
Result can be:
8/10/1999
DecodeTime
Extracts: Hour, Minute, Second, and Millisecond from TDateTime variable, example:
var
Hour, Minute, Second, MSec: word;
TimeStr: string;
begin
DecodeTime(Time, Hour, Minute, Second, MSec);
TimeStr:= IntToStr(Hour)+':'+IntToStr(Minute)+':'+IntToStr(Second)+
'.'+IntToStr(MSec);
ShowMessage(TimeStr);
Result can be:
8:41:49.670
EncodeDate
It is the opposite function to DecodeDate procedure. It collects: Year, Month, and Day
into one TDateTime variable, but you have to make sure that you enter a valid date
or exception will be raised. Example:
var
Year, Month, Day: word;
ADate: TDateTime;
begin
Year:= 1999;
Month:= 9;
Day:= 8;
ADate:= EncodeDate(Year, Month, Day);
ShowMessage(DateToStr(ADate));
end;
EncodeTime
It is the opposite function to DecodeTime procedure. It collects: Hour, Minute, Second,
and Millisecond into one TDateTime variable, but you have to make sure that you enter
a valid time or exception will be raised. Example:
var
Hour, Minute, Second, MSec: word;
ATime: TDateTime;
begin
Hour:= 10;
Minute:= 15;
Second:= 30;
MSec:= 0;
ATime:= EncodeTime(Hour, Minute, Second, MSec);
ShowMessage(TimeToStr(ATime));
Result will be:
10:15:30
StrToDate
Converts date string to TDateTime. Befor conversion make sure that you write a date
according to windows date settings or you can specify a format using ShortDateFormat.
Example:
var
ADate: TDateTime;
begin
ShortDateFormat:= 'dd/mm/yyy';
ADate:= StrToDate('9/10/1999');
Label1.Caption:= DateToStr(ADate);
StrToTime
Converts time string to TDateTime.
Example:
var
ATime: TDateTime;
begin
ATime:= StrToTime('2:23:50 pm');
Label1.Caption:= TimeToStr(ATime);
StrToDateTime
Converts date and time string to TDateTime.
Example:
var
ADateTime: TDateTime;
begin
ShortDateFormat:= 'dd/mm/yyy';
ADateTime:= StrToDateTime('9/10/1999 2:40 pm');
Label1.Caption:= DateTimeToStr(ADateTime);
IncMonth
Increase/Decrease TDateTime variable month or monthes
Examples:
Label1.Caption:= DateTimeToStr(
IncMonth(Now, + 1)); // Next month
Label1.Caption:= DateTimeToStr(
IncMonth(Now, - 2)); // previous two monthes
FormatDateTime
DateTimeToStr, DateToStr, StrToDate, and StrToDateTime functions are using the internal
Windows settings (Regional settings) to convert the string to date and vice versa.
Example:
Suppose that D is a TDateTime variable and it holds the date '15, Dec, 2001' and
Windows date settings is 'mm/dd/yyyy', DateToStr will return:
12/15/2001
If Windows Date settings is 'dd/mm/yyyy' then the result will be:
15/12/2001
In every computer you may get different display for the same date. To unify date
and time display in all computers use DateTimeFormat function:
Label1.Caption:= FormatDateTime('dd/mm/yyyy hh:nn', Now);
d = Day
d = month
y = year
h = Hour
n = Minute
s = Second
mm = Months as a number
mmm = Short month string such as Jun
mmmm = Long month string such as January
ddd = Day of the week as string
See Delphi help for complete listing
Notes:
- String to Date conversion
Using StrToDate and StrToDateTime functions may cause date conversion error. To
understand what exactly causes the error, suppose that you have this statement in
your application:
D:= StrToDate('15/12/2001');
This statement will run without problems in any computer that it's Date setting
is 'dd/mm/yyyy', but it will rais exception in date conversion in the computers that
thier date setting is 'mm/dd/yyyy'. To overcome this problem you can use EncodeDate
function, example:
D:= EncodeDate(2001, 12, 15);
EncodeDate time is working independently of Windows date settings.
Another solution is to adjust your application date setting by using ShortDateFormat
variable, such as:
ShortDateFormat:= 'dd/mm/yyyy';
D:= DateToStr('15/12/2001')
ShortDateFormat will override current Windows date settings.
- Year 2000:
TDateTime is an 8-byte data type and it stores years as a 4 digits, so that THERE
IS NO Y2K PROBLEM when using TDateTime variables.